package com.whg.demo.conf;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * @Author whg
 * @create 2023/3/15 16:01
 * @description：
 */
@Configuration
@ConfigurationProperties
public class DatasourceConfig {

    @Value("${spring.datasource.driver-class-name}")
    private String driverClassName;

    @Value("${spring.datasource.url}")
    private String url;

    @Value("${spring.datasource.username}")
    private String username;

    @Value("${spring.datasource.password}")
    private String password;

    @Bean(name = "xdclass")
    public DataSource getDataSource() throws Exception{
        Properties properties = new Properties();
        properties.setProperty(DruidDataSourceFactory.PROP_DRIVERCLASSNAME,driverClassName);
        properties.setProperty(DruidDataSourceFactory.PROP_URL,url);
        properties.setProperty(DruidDataSourceFactory.PROP_USERNAME,username);
        properties.setProperty(DruidDataSourceFactory.PROP_PASSWORD,password);
        return DruidDataSourceFactory.createDataSource(properties);
    }
}
